export const TEMPERATURE = [0x36, 0x14, 0xC5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x65, 0x42]; //温度
export const ROTATING_SPEED = [0x36, 0x14, 0xC3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xC7, 0xC1]; //转速
export const VIBRATION_JSD = [0x36, 0x14, 0xC1, 0x1, 0x07, 0xA, 0, 0x4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x5D, 0x8C]; //加速度
export const VIBRATION_SD = [0x36, 0x14, 0xC1, 0x1, 0x08, 0xA, 0, 0x4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x5D, 0x8C]; //速度
export const VIBRATION_WY = [0x36, 0x14, 0xC1, 0x1, 0x09, 0xA, 0, 0x4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x5D, 0x8C]; //位移

export function parseResultValue(type, resultBytes) {
	switch (type) {
		case 1:
			return ResultUtils.parseTemperature(resultBytes);
		case 2:
			return ResultUtils.parseSpeed(resultBytes);
		case 3:
		case 4:
		case 5:
			return ResultUtils.parseVibration(resultBytes, type);
	}
	throw new RuntimeException("数据类型错误");
}

class ResultUtils {
	static parseTemperature(data) {
		let byteInt = this.bytesToInt(data[3], data[4], data[5], data[6]);
		console.log('byteInt', byteInt)
		let size = byteInt / 10;
		if (size > 200 || size < -10) {
			byteInt = this.byteArray2Int(data[3], data[4], data[5], data[6]);
		} else if (byteInt < -100) {
			byteInt = -100;
		}
		return (byteInt / 10).toFixed(1);
	}
	static parseSpeed(data, type) {
		const byteInt = this.byteArray2Int(data[5], data[6]);
		return byteInt;
	}
	static parseVibration(data, type) {
		// VibrationResult result = new VibrationResult();
		//         result.resultArray = new float[3];
		//         DecimalFormat df = new DecimalFormat("#.00");
		//         byte up[] = {data[11], data[10], 0, 0};
		//         float record_up = byte2Float(up);//峰值
		//         result.resultArray[0] = Float.parseFloat(df.format(record_up));

		//         byte upup[] = {data[13], data[12], 0, 0};
		//         float record_upup = byte2Float(upup);//峰峰值
		//         result.resultArray[1] = Float.parseFloat(df.format(record_upup));

		//         byte Valid[] = {data[15], data[14], 0, 0};
		//         float record_Valid = byte2Float(Valid);//有效值
		//         result.resultArray[2] = Float.parseFloat(df.format(record_Valid));
		switch (type) {
			case 3: //加速度
			{
				const arrayBuffer = new Uint8Array([data[11], data[10], 0, 0])
				const dataView = new DataView(arrayBuffer.buffer, 0, 4);
				return dataView.getFloat32(0)
			}
			case 4: //速度
			{
				const arrayBuffer = new Uint8Array([data[15], data[14], 0, 0])
				const dataView = new DataView(arrayBuffer.buffer, 0, 4);
				return dataView.getFloat32(0)
			}
			case 5: //位移
			{
				const arrayBuffer = new Uint8Array([data[13], data[12], 0, 0])
				const dataView = new DataView(arrayBuffer.buffer, 0, 4);
				return dataView.getFloat32(0)
			}
		}
		throw new RuntimeException("数据类型错误");
	}
	static bytesToInt(...b) {
		let value = 0;
		console.log('bytesToInt', b)
		switch (b.length) {
			case 1:
				value = ((b[0] & 0xFF) << 24);
				break;
			case 2:
				value = (((b[0] & 0xFF) << 24) |
					((b[1] & 0xFF) << 16));
				break;
			case 3:
				value = (((b[0] & 0xFF) << 24) |
					((b[1] & 0xFF) << 16) |
					((b[2] & 0xFF) << 8));
				break;
			case 4:
				value = (((b[0] & 0xFF) << 24) |
					((b[1] & 0xFF) << 16) |
					((b[2] & 0xFF) << 8) |
					(b[3] & 0xFF));
				break;
		}
		return value;
	}
	static byteArray2Int(...b) {
		let a = 0;
		switch (b.length) {
			case 1:
				a = b[0];
				if (a < 0) {
					a = a + 256;
				}
				break;
			case 2:
				a = ((b[0]) << 8) + b[1];
				if (a < 0) {
					a = a + 256;
				}
				break;
			case 3:
				a = ((b[0]) << 16) + ((b[1]) << 8) + b[2];
				if (a < 0) {
					a = a + 256;
				}
				break;
			case 4:
				a = ((b[0]) << 24) + ((b[1]) << 16) + ((b[2]) << 8) + b[3];
				if (a < 0) {
					a = a + 256;
				}
				break;
		}
		return a;
	}
}